package leetcode.code1471;

import java.util.Arrays;

public class Solution {
	public int[] getStrongest(int[] arr, int k) {
		Arrays.sort(arr);
		int n = arr.length;
		int m = arr[(n - 1) / 2];
		int ans[] = new int[k];
		int p = 0, l = 0, r = n - 1;
		while (k > 0) {
			int L = Math.abs(arr[l] - m), R = Math.abs(arr[r] - m);
			if (L > R || (L == R && arr[l] > arr[r])) {
				ans[p++] = arr[l++];
			} else {
				ans[p++] = arr[r--];
			}
			k--;
		}
		return ans;
	}
}
